Adding some more judges, here and there.
[andmenj-acm.git] / Maratones competidas / Maratón Nacional 2011 / workspace / c / c3.cpp
blobdd82cf27d0446392ab07485c365b74b48ec36c38
1 // Equipo Poncho, Carriel y Ruana
2 using namespace std;
3 #include <algorithm>
4 #include <iostream>
5 #include <iterator>
6 #include <sstream>
7 #include <fstream>
8 #include <cassert>
9 #include <climits>
10 #include <cstdlib>
11 #include <cstring>
12 #include <string>
13 #include <cstdio>
14 #include <vector>
15 #include <cmath>
16 #include <queue>
17 #include <stack>
18 #include <list>
19 #include <map>
20 #include <set>
22 template <class T> string toStr(const T &x)
23 { stringstream s; s << x; return s.str(); }
24 template <class T> int toInt(const T &x)
25 { stringstream s; s << x; int r; s >> r; return r; }
27 #define For(i, a, b) for (int i=(a); i<(b); ++i)
28 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
29 #define D(x) cout << #x " = " << (x) << endl;
31 const double EPS = 1e-9;
33 int cmp(double x, double y = 0, double tol = EPS) {
34 return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
37 #define INPUT_FILE "compression"
39 const int MAXBASES = 105, oo = 1 << 28;
40 int bases[MAXBASES];
42 int B, D;
43 int bit[20]; // bit[i] = index of term i in the document
45 int memo[MAXBASES][1 << 16];
48 int docSize;
50 int f(int b, int mask, int doc) {
51 if (mask == doc) return 0;
52 if (b == B) { // Vi todas las bases
53 return (oo);
55 //assert(b < B);
56 //printf("b = %d\n", b);
57 if (memo[b][mask] != -1) return memo[b][mask];
59 if ((bases[b] | doc) != doc) {
60 return memo[b][mask] = f(b + 1, mask, doc);
63 //assert(compatible(bases[b]));
65 // cojo esta base
66 int ans = f(b + 1, mask | bases[b], doc) + 1;
68 // no la cojo
69 ans = min(ans, f(b + 1, mask, doc));
70 return memo[b][mask] = ans;
73 void solve(int doc) {
75 //For(i, 0, B) if (compatible(bases[i])) printf("Base i=%d: %d\n", i, maskOfBase(bases[i]));
77 // for (int i = 0; i < 16; ++i) {
78 // printf("bit[%d] = %d\n", i, bit[i]);
79 // }
81 int ans = f(0, 0, doc);
82 if (ans >= oo) ans = 0;
83 printf("%d", ans);
86 int main(){
87 freopen(INPUT_FILE ".in", "r", stdin);
88 while (cin >> B >> D) {
89 if (B == 0 and D == 0) break;
90 for (int i = 0; i < B; ++i) {
91 bases[i] = 0;
93 int k; cin >> k;
94 while (k--) {
95 int x; cin >> x; x--;
96 bases[i] |= (1 << x);
98 //printf("base[i=%d] = %d\n", i, bases[i]);
101 for (int j = 0; j < D; ++j) {
102 int doc = 0;
103 cin >> docSize;
104 int k = docSize;
105 while (k--) {
106 int x; cin >> x; x--;
107 doc |= (1 << x);
109 //printf("doc = %d\n", doc);
110 for (int b = 0; b < B; ++b) {
111 for (int mask = 0; mask < (1 << 16); ++mask) {
112 memo[b][mask] = -1;
115 solve(doc);
116 if (j + 1 < D) printf(" ");
118 puts("");
121 return 0;